<template>
    <div>
        {{ info }}
        <h1>会员添加</h1>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td>会员编号</td>
                    <td><input type="text" v-model="info.Code" disabled /></td>
                </tr>
                <tr>
                    <td>手机号</td>
                    <td><input type="text" v-model="info.phone" maxlength="11" /></td>
                </tr>
                <tr>
                    <td>姓名</td>
                    <td><input type="text" v-model="info.Name" /></td>
                </tr>
                <tr>
                    <td>身份证号</td>
                    <td><input type="text" v-model="info.Card" maxlength="18" /></td>
                </tr>
                <tr>
                    <td>会员等级</td>
                    <td>
                        <select v-model="info.LevelName">
                            <option value="">请选择</option>
                            <option :value="item.LevelName" v-for="item in list.a" :key="item.id">{{ item.LevelName }}
                            </option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>会员生日</td>
                    <td><input type="date" v-model="info.BirthDay" /></td>
                </tr>
                <tr>
                    <td>性别</td>
                    <td>
                        <input type="radio" name="sex" v-model="info.Sex" :value="1" />男
                        <input type="radio" name="sex" v-model="info.Sex" :value="0" />女
                    </td>
                </tr>
                <tr>
                    <td>积分</td>
                    <td><input type="text" v-model="info.Score" /></td>
                </tr>
                <tr>
                    <td>余额</td>
                    <td><input type="text" v-model="info.Money" /></td>
                </tr>
                <tr>
                    <td>爱好</td>
                    <td>
                        <input type="checkbox" name="likes" v-model="info.Likes" value="唱歌" />唱歌
                        <input type="checkbox" name="likes" v-model="info.Likes" value="跳舞" />跳舞
                        <input type="checkbox" name="likes" v-model="info.Likes" value="吃" />吃
                        <input type="checkbox" name="likes" v-model="info.Likes" value="牛头" />牛头
                    </td>
                </tr>
                <tr>
                    <td>简介</td>
                    <td>
                        <textarea rows="5" cols="50" v-model="info.Desc"></textarea>
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="button" class="btn btn-success" value="添加" @click="ok" /></td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
//onmounted 生命周期 钩子函数
import axios from 'axios';
//发送网络请求 axios
import { useRouter } from 'vue-router';
import moment from 'moment';
import router from '@/router';
//路由

//声明变量存储后端返回数据
let list: any = reactive({
    a: [],
})


onMounted(() => {
    getList();
});

//声明变量存储数据 存储向后端增加会员时的数据
let info: any = reactive({
    "Code": moment(new Date()).format('YYYYMMDD') +
        Math.random().toString().substring(2, 5), //当前时间
    "phone": '',
    "Name": '',
    "Card": '',
    "LevelName": '',
    "BirthDay": '',
    "Sex": 1,
    "Score": '',
    "Money": '',
    "Likes": [],
    "Desc": '',

})

const ok = () => {
    //验证
    if (info.phone == "") {
        alert("手机号不能为空")
        return;
    }
    let reg = /^1[3-9]\d{9}$/;
    if (reg.test(info.phone) == false) {
        alert("请输入正确手机格式")
        return;
    }
    if (info.Name == "") {
        alert("姓名不能为空");
        return;
    }
    if (info.Card == "") {
        alert("身份证号码不能为空")
        return;
    }
    if (info.LevelName == "") {
        alert("等级名称不能为空");
        return;
    }
    if (info.BirthDay == "") {

        alert("生日不能为空");
        return;
    }
    if (info.Score === "") {
        alert("积分不能为空");
        return;
    }
    //积分正整数
    let reg2 = /^[1-9]\d*$/;
    if (reg2.test(info.Score) == false) {
        alert("积分必须要是正整数");
        return;
    }
    if (info.Money === "") {
        alert("余额不能为空");
        return;
    } if (info.Likes.length == 0) {
        alert("爱好必选")
        return;
    }
    //发送网络请求
    info.Likes = info.Likes.toString();
    axios({
        url: '/api/Menber/MenberAdd',
        method: 'post',
        data: info
    }).then(res => {
        console.log(res);
        if(res.data>0){
            alert("添加成功")
            router.push('/menberList')
        }else if(res.data==-1){
            alert('手机号重复')
            return;
        }
        else{
            alert('修改失败')
        }

    }).catch(err => {

    })
}

const getList = () => {
    axios({
        url: '/api/Menber/GetMenberLevelList',
        method: 'get',
    })
        .then(res => {
            console.log(res);
            // list.value=res.data
            list.a = res.data;
        })
};






</script>

<style scoped></style>